home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -seriously_amiga- / shareware / programming / c / amivogl-mdev / src / drivers.c < prev    next >
C/C++ Source or Header  |  1998-01-12  |  15KB  |  731 lines

  1. #include <stdio.h>
  2. #include "vogl.h"
  3. #include "vodevice.h"
  4.  
  5. /* ---------------------------------------------------------------------
  6.  * Prototypes:
  7.  */
  8. #ifdef __PROTOTYPE__
  9. static void getdev(char *);                            /* drivers.c       */
  10. #else
  11. static void getdev();                                  /* drivers.c       */
  12. #endif
  13.  
  14. /* ---------------------------------------------------------------------
  15.  * Driver Prototypes:
  16.  */
  17. #ifdef __PROTOTYPE__
  18.  
  19. #ifdef AMIGA
  20. void _AMIGA_devcpy(void);                              /* amiga.c         */
  21. #endif
  22.  
  23. #ifdef apollo
  24. int _APOLLO_devcpy(void);                              /* apollo.c        */
  25. #endif
  26.  
  27. #ifdef CGA
  28. int _cga_devcpy(void);                                 /* cga.c           */
  29. #endif
  30.  
  31. #ifdef DECX11
  32. int _DECX11_devcpy(void);                              /* decX11.c        */
  33. #endif
  34.  
  35. #ifdef DXY
  36. int _DXY_devcpy(void);                                 /* hpdxy.c         */
  37. #endif
  38.  
  39. #ifdef EGA
  40. int _ega_devcpy(void);                                 /* ega.c           */
  41. #endif
  42.  
  43. #ifdef GRX
  44. int _grx_devcpy(void);                                 /* grx.c           */
  45. #endif
  46.  
  47. #ifdef HERCULES
  48. #endif
  49.  
  50. #ifdef HGC
  51. int _hgc_devcpy(void);                                 /* hgc.c           */
  52. #endif
  53.  
  54. #ifdef HPGL
  55. int _HPGL_A1_devcpy(void);                             /* hpdxy.c         */
  56. int _HPGL_A2_devcpy(void);                             /* hpdxy.c         */
  57. int _HPGL_A3_devcpy(void);                             /* hpdxy.c         */
  58. int _HPGL_A4_devcpy(void);                             /* hpdxy.c         */
  59. #endif
  60.  
  61. #ifdef NeXT
  62. int _NeXT_devcpy(void);                                /* NeXT.c          */
  63. #endif
  64.  
  65. #ifdef POSTSCRIPT
  66. int _CPS_devcpy(void);                                 /* ps.c            */
  67. int _PS_devcpy(void);                                  /* ps.c            */
  68. int _PSP_devcpy(void);                                 /* ps.c            */
  69. int _PCPS_devcpy(void);                                /* ps.c            */
  70. int _LASER_devcpy(void);                               /* ps.c            */
  71. #endif
  72.  
  73. #ifdef SIGMA
  74. int _sigma_devcpy(void);                               /* sigma.c         */
  75. #endif
  76.  
  77. #ifdef SUN
  78. int _SUN_devcpy(void);                                 /* sun.c           */
  79. #endif
  80.  
  81. #ifdef TEK
  82. int _TEK_devcpy(void);                                 /* tek.c           */
  83. #endif
  84.  
  85. #ifdef VGA
  86. int _vga_devcpy(void);                                 /* vga.c           */
  87. #endif
  88.  
  89. #ifdef X11
  90. int _X11_devcpy(void);                                 /* X11.c           */
  91. #endif
  92.  
  93. #endif    /* __PROTOTYPE__ */
  94.  
  95. /* ---------------------------------------------------------------------
  96.  * Local Variables:
  97.  */
  98. static FILE    *fp        = stdout;
  99. static int     allocated = 0;
  100. struct vdev     vdevice;
  101.  
  102. /* --------------------------------------------------------------------- */
  103.  
  104. /* device-independent function routines */
  105.  
  106. /*
  107.  * voutput
  108.  *
  109.  *    redirect output - only for postscript, hpgl (this is not a feature)
  110.  */
  111. void voutput(char *path)
  112. {
  113. char    buf[128];
  114.  
  115. if ((fp = fopen(path, "w")) == (FILE *)NULL) {
  116.     sprintf(buf, "voutput: couldn't open %s", path);
  117.     verror(buf);
  118.     }
  119. }
  120.  
  121. /* ------------------------------------------------------------------------ */
  122.  
  123. /*
  124.  * _voutfile
  125.  *
  126.  *    return a pointer to the current output file - designed for internal
  127.  * use only.
  128.  */
  129. FILE * _voutfile(void)
  130. {
  131. return(fp);
  132. }
  133.  
  134. /* ------------------------------------------------------------------------ */
  135.  
  136. /*
  137.  * verror
  138.  *
  139.  *    print an error on the graphics device, and then exit. Only called
  140.  * for fatal errors. We assume that stderr is always there.
  141.  *
  142.  */
  143. void verror(char *str)
  144. {
  145. if (vdevice.initialised)
  146. gexit();
  147.  
  148. fprintf(stderr, "%s\n", str);
  149. exit(1);
  150. }
  151.  
  152. /* ------------------------------------------------------------------------ */
  153.  
  154. /*
  155.  * gexit
  156.  *
  157.  *    exit the vogl/vogle system
  158.  *
  159.  */
  160. void gexit(void)
  161. {
  162. if (!vdevice.initialised)
  163. verror("gexit: vogl not initialised");
  164.  
  165. (*vdevice.dev.Vexit)();
  166.  
  167. vdevice.devname = (char *)NULL;
  168. vdevice.initialised = 0;
  169. fp = stdout;
  170. }
  171.  
  172. /* ------------------------------------------------------------------------ */
  173.  
  174. /*
  175.  * getdev
  176.  *
  177.  *    get the appropriate device table structure
  178.  */
  179. static void getdev(char *device)
  180. {
  181.  
  182. #ifdef AMIGA
  183. if      (strncmp(device,"amiga",5) == 0) _AMIGA_devcpy();
  184. else if (strncmp(device,"AMIGA",5) == 0) _AMIGA_devcpy();
  185. else
  186. #endif
  187. #ifdef SUN
  188. if (strncmp(device, "sun", 3) == 0)   _SUN_devcpy();
  189. else
  190. #endif
  191. #ifdef X11
  192. if (strncmp(device, "X11", 3) == 0)   _X11_devcpy();
  193. else
  194. #endif
  195. #ifdef DECX11
  196. if (strncmp(device, "decX11", 6) == 0) _DECX11_devcpy();
  197. else
  198. #endif
  199. #ifdef NeXT
  200. if (strncmp(device, "NeXT", 4) == 0)    _NeXT_devcpy();
  201. else
  202. #endif
  203. #ifdef POSTSCRIPT
  204. if (strncmp(device, "postscript", 10) == 0) {
  205.     _PS_devcpy();
  206.     }
  207. else
  208. if (strncmp(device, "ppostscript", 11) == 0) {
  209.     _PSP_devcpy();
  210.     }
  211. else
  212. #endif
  213. #ifdef HPGL
  214. if (strncmp(device, "hpgla1", 6) == 0)      _HPGL_A1_devcpy();
  215. else if (strncmp(device, "hpgla3", 6) == 0) _HPGL_A3_devcpy();
  216. else if (strncmp(device, "hpgla4", 6) == 0) _HPGL_A4_devcpy();
  217. else if (strncmp(device, "hpgla2", 6) == 0 || strncmp(device, "hpgl", 4) == 0)
  218. _HPGL_A2_devcpy();
  219. else
  220. #endif
  221. #ifdef DXY
  222. if (strncmp(device, "dxy", 3) == 0)         _DXY_devcpy();
  223. else
  224. #endif
  225. #ifdef TEK
  226. if (strncmp(device, "tek", 3) == 0)         _TEK_devcpy();
  227. else
  228. #endif
  229. #ifdef HERCULES
  230. if (strncmp(device, "hercules", 8) == 0)     _hgc_devcpy();
  231. else
  232. #endif
  233. #ifdef CGA
  234. if (strncmp(device, "cga", 3) == 0)          _cga_devcpy();
  235. else
  236. #endif
  237. #ifdef EGA
  238. if (strncmp(device, "ega", 3) == 0)          _ega_devcpy();
  239. else
  240. #endif
  241. #ifdef VGA
  242. if (strncmp(device, "vga", 3) == 0)          _vga_devcpy();
  243. else
  244. #endif
  245. #ifdef SIGMA
  246. if (strncmp(device, "sigma", 5) == 0)        _sigma_devcpy();
  247. else
  248. #endif
  249. {
  250.     if(!device || *device == 0)
  251.     fprintf(stderr,
  252.        "vogl: expected the enviroment variable VDEVICE to be set to the desired device.\n");
  253.     else fprintf(stderr, "vogl: %s is an invalid device type\n", device);
  254.  
  255.     fprintf(stderr, "The devices compiled into this library are:\n");
  256. #ifdef SUN
  257.     fprintf(stderr, "sun\n");
  258. #endif
  259. #ifdef AMIGA
  260.     fprintf(stderr,"amiga\n");
  261. #endif
  262. #ifdef X11
  263.     fprintf(stderr, "X11\n");
  264. #endif
  265. #ifdef DECX11
  266.     fprintf(stderr, "decX11\n");
  267. #endif
  268. #ifdef NeXT
  269.     fprintf(stderr, "NeXT\n");
  270. #endif
  271. #ifdef POSTSCRIPT
  272.     fprintf(stderr, "postscript\n");
  273.     fprintf(stderr, "ppostscript\n");
  274. #endif
  275. #ifdef HPGL
  276.     fprintf(stderr, "hpgla1\n");
  277.     fprintf(stderr, "hpgla2 (or hpgl)\n");
  278.     fprintf(stderr, "hpgla3\n");
  279.     fprintf(stderr, "hpgla4\n");
  280. #endif
  281. #ifdef DXY
  282.     fprintf(stderr, "dxy\n");
  283. #endif
  284. #ifdef TEK
  285.     fprintf(stderr, "tek\n");
  286. #endif
  287. #ifdef HERCULES
  288.     fprintf(stderr, "hercules\n");
  289. #endif
  290. #ifdef CGA
  291.     fprintf(stderr, "cga\n");
  292. #endif
  293. #ifdef EGA
  294.     fprintf(stderr, "ega\n");
  295. #endif
  296. #ifdef VGA
  297.     fprintf(stderr, "vga\n");
  298. #endif
  299. #ifdef SIGMA
  300.     fprintf(stderr, "sigma\n");
  301. #endif
  302.     exit(1);
  303.     }
  304. }
  305.  
  306. /* ------------------------------------------------------------------------ */
  307.  
  308. /*
  309.  * vinit
  310.  *
  311.  *     Just set the device name. ginit and winopen are basically
  312.  * the same as the VOGLE the vinit function.
  313.  *
  314.  */
  315. void vinit(char *device)
  316. {
  317. vdevice.devname = device;
  318. }
  319.  
  320. /* ------------------------------------------------------------------------ */
  321.  
  322. /*
  323.  * winopen
  324.  *
  325.  *    use the more modern winopen call (this really calls ginit),
  326.  * we use the title if we can
  327.  */
  328. long winopen(char *title)
  329. {
  330.  
  331. vdevice.wintitle = title;
  332.  
  333. ginit();
  334.  
  335. return(1L);
  336. }
  337.  
  338. /* ------------------------------------------------------------------------ */
  339.  
  340. /*
  341.  * ginit
  342.  *
  343.  *    by default we check the environment variable, if nothing
  344.  * is set we use the value passed to us by the vinit call.
  345.  */
  346. void ginit(void)
  347. {
  348. char    *dev= NULL;
  349. int    i;
  350.  
  351.  
  352. if (vdevice.devname == (char *)NULL) {
  353.     if ((dev = getenv("VDEVICE")) == (char *)NULL) getdev("");
  354.     else                                           getdev(dev);
  355.     }
  356. else                                               getdev(vdevice.devname);
  357.  
  358. if (vdevice.initialised) gexit();
  359.  
  360. if (!allocated) {
  361.     allocated              = 1;
  362.     vdevice.transmat       = (Mstack *)vallocate(sizeof(Mstack));
  363.     vdevice.transmat->back = (Mstack *)NULL;
  364.     vdevice.attr           = (Astack *)vallocate(sizeof(Astack));
  365.     vdevice.attr->back     = (Astack *)NULL;
  366.     vdevice.viewport       = (Vstack *)vallocate(sizeof(Vstack));
  367.     vdevice.viewport->back = (Vstack *)NULL;
  368.     vdevice.bases          = (Matrix *)vallocate(sizeof(Matrix) * 10);
  369.     vdevice.enabled        = (char *)vallocate(MAXDEVTABSIZE);
  370.     }
  371.  
  372. for (i = 0; i < MAXDEVTABSIZE; i++) vdevice.enabled[i] = 0;
  373.  
  374. vdevice.alreadyread      = vdevice.data        = vdevice.devno     = 0;
  375. vdevice.kbdmode          = vdevice.mouseevents = vdevice.kbdevents = 0;
  376.  
  377. vdevice.clipoff          = 0;
  378. vdevice.cpW[V_W]         = 1.0;            /* never changes */
  379.  
  380. vdevice.maxfontnum       = 2;
  381.  
  382. vdevice.attr->a.fontnum  = 0;
  383. vdevice.attr->a.mode     = 0;
  384. vdevice.attr->a.backface = 0;
  385.  
  386. if ((*vdevice.dev.Vinit)()) {
  387.     vdevice.initialised = 1;
  388.  
  389.     viewport((Screencoord)0, (Screencoord)vdevice.sizeSx,
  390.     (Screencoord)0, (Screencoord)vdevice.sizeSy);
  391.  
  392.     ortho2(0.0, (Coord)vdevice.sizeSx, 0.0, (Coord)vdevice.sizeSy);
  393.  
  394.     /*
  395.     identmatrix(vdevice.transmat->m);
  396.     _mapmsave(vdevice.transmat->m);
  397.     */
  398.  
  399.     move(0.0, 0.0, 0.0);
  400.  
  401.     font((short) 0);    /* set up default font */
  402.  
  403.     vdevice.inobject = 0;
  404.     vdevice.inpolygon = 0;
  405.     }
  406. else {
  407.     fprintf(stderr, "vogl: error while setting up device\n");
  408.     exit(1);
  409.     }
  410.  
  411. vdevice.alreadyread = 0;
  412. vdevice.mouseevents = 0;
  413. vdevice.kbdevents = 0;
  414. vdevice.kbdmode = 0;
  415.  
  416. vdevice.concave = 0;
  417.  
  418. }
  419.  
  420. /* ------------------------------------------------------------------------ */
  421.  
  422. /*
  423.  * gconfig
  424.  *
  425.  *    thankfully a noop.
  426.  */
  427. void gconfig(void)
  428. {
  429. }
  430.  
  431. /* ------------------------------------------------------------------------ */
  432.  
  433. /*
  434.  * vnewdev
  435.  *
  436.  * reinitialize vogl to use a new device but don't change any
  437.  * global attributes like the window and viewport settings.
  438.  */
  439. void vnewdev(char *device)
  440. {
  441. if (!vdevice.initialised)
  442. verror("vnewdev: vogl not initialised\n");
  443.  
  444. pushviewport();    
  445.  
  446. (*vdevice.dev.Vexit)();
  447.  
  448. vdevice.initialised = 0;
  449.  
  450. getdev(device);
  451.  
  452. (*vdevice.dev.Vinit)();
  453.  
  454. vdevice.initialised = 1;
  455.  
  456. /*
  457.  * Need to update font for this device...
  458.  */
  459. font((short) vdevice.attr->a.fontnum);
  460.  
  461.  
  462. popviewport();
  463. }
  464.  
  465. /* ------------------------------------------------------------------------ */
  466.  
  467. /*
  468.  * vgetdev
  469.  *
  470.  *    Returns the name of the current vogl device 
  471.  *    in the buffer buf. Also returns a pointer to
  472.  *    the start of buf.
  473.  */
  474. char * vgetdev(char *buf)
  475. {
  476. /*
  477.  * Note no exit if not initialized here - so that gexit
  478.  * can be called before printing the name.
  479.  */
  480. if (vdevice.dev.devname)
  481. strcpy(buf, vdevice.dev.devname);
  482. else
  483. strcpy(buf, "(no device)");
  484.  
  485. return(&buf[0]);
  486. }
  487.  
  488. /* ------------------------------------------------------------------------ */
  489.  
  490. /*
  491.  * getvaluator
  492.  *
  493.  *    similar to the VOGLE locator only it returns either x (MOUSEX) or y (MOUSEY).
  494.  */
  495. long getvaluator(Device dev)
  496. {
  497. int    a, b, c;
  498.  
  499. if (!vdevice.initialised)
  500. verror("getvaluator: vogl not initialised");
  501.  
  502. c = (*vdevice.dev.Vlocator)(&a, &b);
  503.  
  504. if (c != -1) {
  505.     if (dev == MOUSEX)
  506.     return((long)a);
  507.     else 
  508.     return((long)b);
  509.     }
  510.  
  511. return(-1);
  512. }
  513.  
  514. /* ------------------------------------------------------------------------ */
  515.  
  516. /*
  517.  * getbutton
  518.  *
  519.  *    returns the up (or down) state of a button. 1 means down, 0 up,
  520.  * -1 invalid.
  521.  */
  522. Boolean getbutton(Device dev)
  523. {
  524. int    a, b, c;
  525.  
  526. if (dev < 256) {
  527.     c = (*vdevice.dev.Vcheckkey)();
  528.     if (c >= 'a' && c <= 'z')
  529.     c = c - 'a' + 'A';
  530.     if (c == dev)
  531.     return(1);
  532.     return(0);
  533.     }
  534. else if (dev < 261) {
  535.     c = (*vdevice.dev.Vlocator)(&a, &b);
  536.     if (c & 0x01 && dev == MOUSE3)
  537.     return(1);
  538.     if (c & 0x02 && dev == MOUSE2)
  539.     return(1);
  540.     if (c & 0x04 && dev == MOUSE1)
  541.     return(1);
  542.     return(0);
  543.     }
  544.  
  545. return(-1);
  546. }
  547.  
  548. /* ------------------------------------------------------------------------ */
  549.  
  550. /*
  551.  * clear
  552.  *
  553.  *    clears the screen to the current colour, excepting devices
  554.  * like a laser printer where it flushes the page.
  555.  *
  556.  */
  557. void clear(void)
  558. {
  559. Token    *tok;
  560.  
  561. if (!vdevice.initialised)
  562. verror("clear: vogl not initialised");
  563.  
  564. if (vdevice.inobject) {
  565.     tok = newtokens(1);
  566.     tok->i = CLEAR;
  567.  
  568.     return;
  569.     }
  570.  
  571. (*vdevice.dev.Vclear)();
  572. }
  573.  
  574. /* ------------------------------------------------------------------------ */
  575.  
  576. /*
  577.  * colorf
  578.  *
  579.  *    set the current colour to colour index given by
  580.  * the rounded value of f.
  581.  *
  582.  */
  583. void colorf(float f)
  584. {
  585. color((int)(f + 0.5));
  586. }
  587.  
  588. /* ------------------------------------------------------------------------ */
  589.  
  590. /*
  591.  * color
  592.  *
  593.  *    set the current colour to colour index number i.
  594.  *
  595.  */
  596. void color(int i)
  597. {
  598. Token    *tok;
  599.  
  600. if (!vdevice.initialised)
  601. verror("color: vogl not initialised");
  602.  
  603. if (vdevice.inobject) {
  604.     tok = newtokens(2);
  605.  
  606.     tok[0].i = COLOR;
  607.     tok[1].i = i;
  608.     return;
  609.     }
  610.  
  611. vdevice.attr->a.color = i;
  612. (*vdevice.dev.Vcolor)(i);
  613. }
  614.  
  615. /* ------------------------------------------------------------------------ */
  616.  
  617. /*
  618.  * mapcolor
  619.  *
  620.  *    set the color of index i.
  621.  */
  622. void mapcolor(
  623.   Colorindex i,
  624.   short r,
  625.   short g,
  626.   short b)
  627. {
  628. Token    *tok;
  629.  
  630. if (!vdevice.initialised)
  631. verror("mapcolor: vogl not initialised");
  632.  
  633. if (vdevice.inobject) {
  634.     tok = newtokens(5);
  635.  
  636.     tok[0].i = MAPCOLOR;
  637.     tok[1].i = i;
  638.     tok[2].i = r;
  639.     tok[3].i = g;
  640.     tok[4].i = b;
  641.  
  642.     return;
  643.     }
  644.  
  645. (*vdevice.dev.Vmapcolor)((int) i, (int) r, (int) g, (int) b);
  646. }
  647.  
  648. /* ------------------------------------------------------------------------ */
  649.  
  650. /*
  651.  * getplanes
  652.  *
  653.  *    Returns the number if bit planes on a device.
  654.  */
  655. long getplanes(void)
  656. {
  657. if (!vdevice.initialised)
  658. verror("getdepth: vogl not initialised\n");
  659.  
  660. return((long)vdevice.depth);
  661. }
  662.  
  663. /* ------------------------------------------------------------------------ */
  664.  
  665. /*
  666.  * reshapeviewport
  667.  *        - does nothing
  668.  */
  669. void reshapeviewport(void)
  670. {
  671. }
  672.  
  673. /* ------------------------------------------------------------------------ */
  674.  
  675. /*
  676.  * winconstraints
  677.  *        - does nothing
  678.  */
  679. void winconstraints(void)
  680. {
  681. }
  682.  
  683. /* ------------------------------------------------------------------------ */
  684.  
  685. /*
  686.  * keepaspect
  687.  *        - does nothing
  688.  */
  689. void keepaspect(void)
  690. {
  691. }
  692.  
  693. /* ------------------------------------------------------------------------ */
  694.  
  695. /*
  696.  * shademodel
  697.  *        - does nothing
  698.  */
  699. void shademodel(long model)
  700. {
  701. }
  702.  
  703. /* ------------------------------------------------------------------------ */
  704.  
  705. /*
  706.  * getgdesc
  707.  *
  708.  *    Inquire about some stuff....
  709.  */
  710. long getgdesc(long inq)
  711. {
  712. /*
  713.  * How can we know before the device is inited??
  714.  */
  715.  
  716. switch (inq) {
  717. case GD_XPMAX:
  718.     if (vdevice.initialised) return (long)vdevice.sizeSx;
  719.     else                     return 500L;    /* A bullshit number */
  720. case GD_YPMAX:
  721.     if (vdevice.initialised) return((long)vdevice.sizeSy);
  722.     else                     return 500L;
  723. default:
  724.     return -1L;
  725.     }
  726. /* return -1L; statement never reached */
  727. }
  728.  
  729. /* ------------------------------------------------------------------------ */
  730.  
  731.